package com.da.javatest.design.schema.singleton;

public class SingletonTest04 {
    private static SingletonTest04 instance;

    private SingletonTest04() {}

    //提供一个静态的公有方法，加入同步处理的代码，解决线程安全问题
    //即懒汉式
    public synchronized static SingletonTest04 getInstance() {
        if (instance == null) {
            instance = new SingletonTest04();
        }
        return instance;
    }

    public static void main(String[] args) {
        System.out.println("懒汉式2 ， 线程安全~");
        SingletonTest04 instance = SingletonTest04.getInstance();
        SingletonTest04 instance2 = SingletonTest04.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}
